/**
 * Desc
 * @description Holp You Do Good But Not Evil
 * @copyright   Copyright 2014-2015 <ycchen@iwshop.cn>
 * @license     LGPL (http://www.gnu.org/licenses/lgpl.html)
 * @author      Chenyong Cai <ycchen@iwshop.cn>
 * @package     Wshop
 * @link        http://www.iwshop.cn
 */

requirejs(['jquery', 'util', 'fancyBox', 'datatables'], function ($, util, fancyBox, dataTables) {
    $(function () {

        var dt = $('.dTable').dataTable(DataTableConfig).api();

        fnFancyBox('#add-level,.add-level', function () {

            $('.expprovince label').unbind('click').click(function () {
                $(this).parent().find('input').click();
            });

            $('#al-com-save').unbind('click').click(function () {

                var auth = [];
                $('#authList input').each(function () {
                    if ($(this).get(0).checked) {
                        auth.push($(this).val());
                    }
                });

                var cid = parseInt($(this).attr('data-id'));

                $.post('?/wSettings/addAuth/', {
                    id: cid > 0 ? cid : '',
                    acc: $('#acc').val(),
                    pwd: $('#pwd').val(),
                    auth: auth.join(',')
                }, function (res) {
                    if (res > 0) {
                        $.fancybox.close();
                        location.reload();
                        util.Alert('修改成功');
                    } else {
                        util.Alert('修改失败', true);
                    }
                });
            });
        });

        $('.envs_del').click(function () {
            if (confirm('你确认要删除么')) {
                var node = $(this);
                $.post('?/wSettings/deleteAuth/', {
                    id: $(this).attr('data-id')
                }, function (res) {
                    if (res > 0) {
                        util.Alert('删除成功');
                        dt.row(node.parents('tr')).remove().draw();
                    } else {
                        util.Alert('操作失败!', true);
                    }
                });
            }
        });

    });
});